package net.fdukedom.o3i.icq;

import java.util.Arrays;

/**
 *  This class describes single FLAP item.
 * 
 * @author duke
 */
public class Flap {
    
    /**
     * FLAP headers size in bytes.
     */
    public static final int HEADERS_SIZE = 6;
            
    /**
     * Command start value.
     */
    public static final byte COMMAND_START = 0x2A;
    
    /**
     * Command start. Is always 0x2A.
     */
    private final byte commandStart = COMMAND_START;
    
    /**
     * ID of channel. It may vary from 1 to 4.
     */
    private byte channelId;
    
    /**
     * Sequence number.
     */
    private short sequenceNumber;
    
    /**
     * FLAP data. Usually it is encoded SNAC.
     */
    private byte[] data;
    
    /**
     * Data length in bytes or SNAC size in bytes.
     */
    private int dataLength;
    
    /**
     * SNAC item.
     */
    private Snac snac;
    
    /**
     * If FLAP contains SNAC or raw data.
     */
    private boolean containsSnac;

    /**
     * Create single FLAP from all required information.
     * 
     * @param channelId channel id
     * @param sequenceNumber sequence number
     * @param data binary data
     */
    public Flap(byte channelId, short sequenceNumber, byte[] data) {
        this.channelId = channelId;
        this.sequenceNumber = sequenceNumber;
        this.data = Arrays.copyOf(data, data.length);
        dataLength = data.length;
    }

    /**
     * Create single FLAP from all required information and decoded from data SNAC.
     * 
     * @param channelId channel id
     * @param sequenceNumber sequence number
     * @param data binary data
     * @param snac decoded SNAC
     */
    public Flap(byte channelId, short sequenceNumber, byte[] data, Snac snac) {
        this.channelId = channelId;
        this.sequenceNumber = sequenceNumber;
        this.snac = snac;
        this.data = Arrays.copyOf(data, data.length);
        dataLength = Snac.HEADERS_SIZE + snac.getData().length;
    }

    public byte getCommandStart() {
        return commandStart;
    }

    public boolean containsSnac() {
        return containsSnac;
    }        
    
    public byte getChannelId() {
        return channelId;
    }

    public byte[] getData() {
        return data;
    }
    
    public int getDataLength() {
        return dataLength;
    }

    public short getSequenceNumber() {
        return sequenceNumber;
    }

    public Snac getSnac() {
        return snac;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Flap other = (Flap) obj;
        if (this.commandStart != other.commandStart) {
            return false;
        }
        if (this.channelId != other.channelId) {
            return false;
        }
        if (this.sequenceNumber != other.sequenceNumber) {
            return false;
        }
        if (this.data != other.data && (this.data == null || !this.data.equals(other.data))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 59 * hash + this.commandStart;
        hash = 59 * hash + this.channelId;
        hash = 59 * hash + this.sequenceNumber;
        return hash;
    }
    
    
}
